package com.xy.etl.ibatis.node.impl;

import com.xy.etl.ibatis.core.XySQLExePara;
import com.xy.etl.ibatis.core.XySQLTaskContext;
import com.xy.etl.ibatis.node.XySQLSuperNode;

import java.util.ArrayList;
import java.util.List;

public class XySQLChooseNode extends XySQLSuperNode {

    @Override
    public String getTag() {
        return "choose";
    }

    @Override
    public XySQLSuperNode clone() {
        return new XySQLChooseNode();
    }


    public List<XySQLExePara> boundSql(XySQLTaskContext task) throws Exception {

        List<XySQLSuperNode> subList = this.getChildList();
        if (subList == null || subList.size() == 0) {
            return new ArrayList<>();
        }
        //依次判断
        for (XySQLSuperNode node : subList) {
            if (node instanceof XySQLWhenNode) {
                if (((XySQLWhenNode) node).pass(task)) {
                    return node.boundSql(task);
                }
                continue;
            } else if (node instanceof XySQLOtherwiseNode) {
                return node.boundSql(task);
            }
        }
        return new ArrayList<>();
    }


}
